﻿#define _CRT_SECURE_NO_WARNINGS 1


//
////https://leetcode.cn/problems/peak-index-in-a-mountain-array/description/
////852. 山脉数组的峰顶索引
//class Solution {
//public:
//    int peakIndexInMountainArray(vector<int>& arr)
//    {
//        int left = 0, right = arr.size() - 1;
//        while (left < right)
//        {
//            int mid = left + (right - left + 1) / 2;
//            if (arr[mid] > arr[mid - 1]) left = mid;
//            else right = mid - 1;
//        }
//        return left;
//    }
//};


//162. 寻找峰值
//https://leetcode.cn/problems/find-peak-element/
class Solution {
public:
    int findPeakElement(vector<int>& nums)
    {
        int left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < nums[mid + 1]) left = mid + 1;
            else if (nums[mid] > nums[mid + 1]) right = mid;
        }
        return left;
    }
};


//https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/
//搜索旋转排序数组中的最⼩值（medium）

class Solution {
public:
    int findMin(vector<int>& nums)
    {
        int left = 0, right = nums.size() - 1;
        int x = nums[right];
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] > x) left = mid + 1;
            else right = mid;
        }
        return nums[left];
    }
};






//0〜n-1 中缺失的数字（easy）
//https://leetcode.cn/problems/que-shi-de-shu-zi-lcof/
class Solution {
public:
    int takeAttendance(vector<int>& records)
    {
        int left = 0, right = records.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (records[mid] == mid) left = mid + 1;
            else right = mid;
        }
        return records[left] == left ? left + 1 : left;

    }
};